// pages/data/data.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    hidden: true,
    allMoney: "",
    allNumber: "",
    i: 0,
    hidden1: true
  },
  bindsubmit: function (evt) {
    let name = evt.detail.value.name
    let event = evt.detail.value.event
    let money = evt.detail.value.money
    this.setData({
      name: name,
      event: event,
      money: money
    })
    var that = this; //这句不能少，在查询时
    wx.showLoading({
      title: '查询中...',
    })
    // const database = wx.cloud.database()
    wx.cloud.callFunction({
      name: 'getList',
      data: {
        name: name,
        event: event,
        money: money,
        key: "0"
      },
      success(res) {
        var all = res.result.data
        console.log(all)
        if (all.length != 0) {
          wx.hideLoading()
          wx.showToast({
            title: '查询成功',
            image: '../../images/app/successTips.png',
          })
          that.setData({
            arr: all,
            hidden: false,
            hidden1: false
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '查询为空',
            image: '../../images/app/destructionTips.png',
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.hideLoading()
        wx.showToast({
          title: '查询为空',
          image: '../../images/app/destructionTips.png',
        })
        return res
      }
    })

  },
  // 删除记录
  remove: function (i) {
    console.log(i)
    var id = i.currentTarget.dataset.id
    var index = i.currentTarget.dataset.index
    console.log(index)
    wx.showLoading({
      title: '删除中...',
    })
    var that = this
    var id = id
    var arr1 = that.data.arr
    console.log(arr1)
    const db = wx.cloud.database()
    // remove 删除操作
    // primary key 要删除的那条数据的主键id
    db.collection('add').doc(id)
      .remove()
      .then(res => {
        // 删除数据成功
        wx.hideLoading()
        arr1.splice(index, 1)
        that.setData({
          arr: arr1
        })
        console.log(arr1)
        if (arr1.length == 0) {
          that.setData({
            hidden: true,
            hidden1:true
          })
        }
        wx.showToast({
          title: '删除成功',
          image: '../../images/app/successTips.png',
        })

      }).catch(err => {
        // 删除数据失败
        wx.hideLoading()
        wx.showToast({
          title: '删除失败',
          image: '../../images/app/destructionTips.png',
        })
        console.log(err)
      })

  },
  jsallMoney: function () {
    var allMoney1 = 0
    var allNumber1 = this.data.arr.length
    for (var i = 0; i < this.data.arr.length; i++) {
      allMoney1 = allMoney1 + parseInt(this.data.arr[i].money)
    }
    console.log(allMoney1)
    this.setData({
      allMoney: allMoney1,
      allNumber: allNumber1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var arr1 = []
    this.setData({
      arr: arr1,
      i: 0,
      allMoney: "",
      allNumber: "",
      hidden1: true,
      hidden: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.i++
    var name1 = this.data.name
    var event1 = this.data.event
    var money1 = this.data.money
    console.log(this.data.i)
    var that = this; //这句不能少，在查询时
    wx.showLoading({
      title: '查询中...',
    })
    // const database = wx.cloud.database()
    wx.cloud.callFunction({
      name: 'getList',
      data: {
        name: name1,
        event: event1,
        money: money1,
        key: "1",
        i: this.data.i
      },
      success(res) {
        var all = res.result.data
        console.log(all.length)
        if (all.length != 0) {
          wx.hideLoading()
          wx.showToast({
            title: '查询成功',
            image: '../../images/app/successTips.png',
          })
          that.setData({
            arr: that.data.arr.concat(all),
            hidden: false
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '查询为空',
            image: '../../images/app/destructionTips.png',
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.hideLoading()
        wx.showToast({
          title: '查询为空',
          image: '../../images/app/destructionTips.png',
        })
        return res
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})